package org.deng.vertx.springboot.web.controller;

import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.auth.jwt.JWTAuth;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import lombok.extern.slf4j.Slf4j;
import org.deng.vertx.springboot.pojo.ResData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author: DengLibin
 * @Date: Create in 2022-04-30 15:08:35
 * @Description:
 */
@Slf4j
@Component
public class LoginController extends BaseController {

    private JWTAuth  jwtAuth;

    @Autowired
    public void setJwtAuth(JWTAuth jwtAuth) {
        this.jwtAuth = jwtAuth;
    }

    @Override
    public void configRouter(Router router) {
        log.info("LoginController");
        router.route("/login").method(HttpMethod.GET).blockingHandler(this::login);
    }

    private void login(RoutingContext ctx) {
        if ("denglibin".equals(ctx.request().getParam("username")) &&
                        "denglibin".equals(ctx.request().getParam("password"))) {
            //生成token
            String token = jwtAuth.generateToken(new JsonObject().put("userId", "23232342342567"));
            writeJSON(ctx, ResData.ok(token));
        } else {
            writeJSON(ctx, ResData.error("用户名或密码错误"));
        }

    }


}
